/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.informet.ipd.modelo.dao;

import com.informet.ipd.modelo.vo.MObjetivoVO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author LAPTOP01
 */
public class MObjetivoDao extends DBase {

    Connection conn = null;

    public MObjetivoDao() {
        super();
    }

    private void setMObjetivo(MObjetivoVO bean, ResultSet rs) throws SQLException {
        bean.setNUM_CODOBJ_PK(rs.getInt("NUM_CODOBJ_PK"));
        bean.setNUM_CODMETAO_PK(rs.getInt("NUM_CODMETAO_PK"));
        bean.setTX_DESCMETAO(rs.getString("TX_DESCMETAO"));
        bean.setTX_TIPOMETAO(rs.getString("TX_TIPOMETAO"));
        bean.setNUM_IDMOBJ(rs.getInt("NUM_IDMOBJ"));
        bean.setNUM_IDOBJ(rs.getInt("NUM_IDOBJ"));
    }

    public String spMan(int tipo, MObjetivoVO bean) {
        String msj = "";
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            CallableStatement cs = conn.prepareCall("{call Man_METAOBJ(?,?,?,?,?,?)}");
            cs.setInt(1, tipo);
            cs.setInt(2, bean.getNUM_CODMETAO_PK());
            cs.setString(3, bean.getTX_TIPOMETAO());
            cs.setString(4, bean.getTX_DESCMETAO());
            cs.setInt(5, bean.getNUM_CODOBJ_PK());
            cs.setInt(6, bean.getNUM_IDMOBJ());
            cs.execute();



        } catch (Exception e) {
            msj = e.toString();
            return msj;
        }
        return msj;
    }

    public ArrayList<MObjetivoVO> selectLista() throws SQLException {

        Statement stmt = null;
        ResultSet rs = null;
        ArrayList<MObjetivoVO> lista = new ArrayList<MObjetivoVO>(0);
        //lista=null;
        String sql = "";
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            stmt = conn.createStatement();
            sql = "select NUM_CODMETAO_PK,TX_DESCMETAO from ME_METAOBJ";
            rs = stmt.executeQuery(sql);
            MObjetivoVO bean = null;
            while (rs.next()) {
                bean = new MObjetivoVO();
                setMObjetivo(bean, rs);
                lista.add(bean);
            }

        } finally {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (stmt != null) {
                stmt.close();
                stmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }

        }
        return lista;
    }

    public List getlist() throws SQLException {

        Statement stmt = null;
        ResultSet rs = null;
        ArrayList<MObjetivoVO> lista = new ArrayList<MObjetivoVO>(0);
        //lista=null;
        String sql = "";
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            stmt = conn.createStatement();
            sql = "select NUM_CODOBJ_PK,NUM_CODMETAO_PK,TX_DESCMETAO,TX_TIPOMETAO from ME_METAOBJ";
            rs = stmt.executeQuery(sql);
            MObjetivoVO bean = null;
            while (rs.next()) {
                bean = new MObjetivoVO();
                setMObjetivo(bean, rs);
                lista.add(bean);
            }

        } finally {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (stmt != null) {
                stmt.close();
                stmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }

        }
        return lista;
    }

    public List getlist(int cod) throws SQLException {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        ArrayList<MObjetivoVO> lista = new ArrayList<MObjetivoVO>(0);
        //lista=null;
        String sql = "";
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            sql = "select mobj.NUM_CODMETAO_PK,obj.NUM_IDOBJ,mobj.NUM_IDMOBJ,mobj.TX_TIPOMETAO,mobj.TX_DESCMETAO,mobj.NUM_CODOBJ_PK from ME_METAOBJ mobj inner join ME_OBJ obj on obj.NUM_CODOBJ_PK=mobj.NUM_CODOBJ_PK  where mobj.NUM_CODOBJ_PK=? order by NUM_IDMOBJ";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, cod);
            rs = pstmt.executeQuery();
            MObjetivoVO bean = null;
            while (rs.next()) {
                bean = new MObjetivoVO();
                setMObjetivo(bean, rs);
                lista.add(bean);
            }

        } finally {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (pstmt != null) {
                pstmt.close();
                pstmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }

        }
        return lista;
    }

    public MObjetivoVO selectById(int cod, int id) throws SQLException {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Connection conn = null;
        MObjetivoVO bean = null;
        try {
            conn = DriverManager.getConnection(url, usuario, password);
            String sql = "select * from ME_METAOBJ where NUM_CODMETAO_PK=? and NUM_CODMETAO_PK=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, cod);
            pstmt.setInt(2, id);
            rs = pstmt.executeQuery();
            bean = new MObjetivoVO();
            if (rs.next()) {
                setMObjetivo(bean, rs);
            }
            rs.close();
            pstmt.close();
        } finally {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (pstmt != null) {
                pstmt.close();
                pstmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }
        }

        return bean;
    }
}
